import Primitives from './lib/primitives';

export default class ControlFly {
  constructor(basicCesium) {
     this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;
    
    this.lng = 34.6169472
    this.lat = 47.6458037
    this.primitives = new Primitives(this.viewer);
    
    this.primitive = null;
    //初始状态
    this.headingPitchRoll = null;
    // 局部变换坐标系
    this.fixedFrameTransform = null;
    // 速度
    this.speed = 100;

    this.movePath = null;
    this.paths = null;

    this.initRender();
    this.initEntity()
  }

  getPosition() {
    return this.basicCesium.lnglatToWorld(this.lng, this.lat, 500);
  }

  initEntity() {
    if (this.entity != null) return false;
    let cartesian = this.getPosition();
    this.headingPitchRoll = new Cesium.HeadingPitchRoll();
    this.fixedFrameTransform =
      Cesium.Transforms.localFrameToFixedFrameGenerator("north", "west");

    this.primitive = this.primitives.createPrimitiveModel({
      path: 'glb/drone.glb',
      position: cartesian,
      headingPitchRoll: this.headingPitchRoll,
      fixedFrameTransform: this.fixedFrameTransform,
    });
    this.primitive.minimumPixelSize = 1000;

    this.paths = new Cesium.SampledPositionProperty();
    this.movePath = this.basicCesium.createPath(this.paths)

    this.initListener();
  }

  initListener() {
    if (this.primitive == null) return;
    // 每次操作姿态变化为5°
    let deltaRadians = Cesium.Math.toRadians(5.0);
    window.addEventListener(
      "keydown",
      (e) => {
        console.log('keyCode====>', e.keyCode);
        switch (e.keyCode) {
          case 87: //w
            this.headingPitchRoll.pitch += deltaRadians;
            // 判断是否超过2π范围
            if (this.headingPitchRoll.pitch > Cesium.Math.TWO_PI) {
              this.headingPitchRoll.pitch -= Cesium.Math.TWO_PI;
            }
            console.log("抬头：pitch+");
            break;
          case 83: //s
            this.headingPitchRoll.pitch -= deltaRadians;
            if (this.headingPitchRoll.pitch < -Cesium.Math.TWO_PI) {
              this.headingPitchRoll.pitch += Cesium.Math.TWO_PI;
            }
            console.log("低头：pitch-");
            break;
          case 65: //a
            this.headingPitchRoll.heading -= deltaRadians;
            // 判断是否超过2π范围
            if (this.headingPitchRoll.heading < -Cesium.Math.TWO_PI) {
              this.headingPitchRoll.heading += Cesium.Math.TWO_PI;
            }
            console.log("左转：heading+");
            break;
          case 68: //d
            this.headingPitchRoll.heading += deltaRadians;
            // 判断是否超过2π范围
            if (this.headingPitchRoll.heading > Cesium.Math.TWO_PI) {
              this.headingPitchRoll.heading -= Cesium.Math.TWO_PI;
            }
            console.log("右转：heading-");
            break;
          case 38: //上
            this.headingPitchRoll.roll += deltaRadians;
            // 判断是否超过2π范围
            if (this.headingPitchRoll.roll > Cesium.Math.TWO_PI) {
              this.headingPitchRoll.roll -= Cesium.Math.TWO_PI;
            }
            console.log("顺时针翻滚：roll+");
            break;
          case 40: //下
            this.headingPitchRoll.roll -= deltaRadians;
            // 判断是否超过2π范围
            if (this.headingPitchRoll.roll < -Cesium.Math.TWO_PI) {
              this.headingPitchRoll.roll += Cesium.Math.TWO_PI;
            }
            console.log("逆时针翻滚：roll-");
            break;
          case 39: //右
            this.speed += 10;
            this.speed = Math.min(this.speed, 1000);
            console.log("加速:" + this.speed);
            break;
          case 37: //左
            this.speed -= 10;
            this.speed = Math.max(this.speed, 100);
            console.log("减速:" + this.speed);
            break;
          case 27: //左
            this.basicCesium.transformLookAt();
            break;
        }
      },
      false
    );
  }

  initRender() {
    // 速度向量
    let speedVector = new Cesium.Cartesian3();
    let position = this.getPosition();
    console.log(this.primitive)
    this.viewer.scene.preUpdate.addEventListener(() => {
      speedVector = Cesium.Cartesian3.multiplyByScalar(
        Cesium.Cartesian3.UNIT_X,
        this.speed / 10,
        speedVector
      );
      position = Cesium.Matrix4.multiplyByPoint(
        this.primitive.modelMatrix,
        speedVector,
        position
      );
      this.paths.addSample(Cesium.JulianDate.now(), position);
      // 更新模型姿态与位置
      Cesium.Transforms.headingPitchRollToFixedFrame(
        position,
        this.headingPitchRoll,
        Cesium.Ellipsoid.WGS84,
        this.fixedFrameTransform,
        this.primitive.modelMatrix
      );

    });
  }
}